package biz

import (
	"context"
	"fmt"
	"gitee.com/shuokeyun/order-service/internal/domain"
	"time"
)

var _ domain.OrderUsecase = (*orderUsecase)(nil)

type orderUsecase struct {
	orderRepo domain.OrderRepo
}

func NewOrderUsecase(repo domain.OrderRepo) domain.OrderUsecase {
	return &orderUsecase{
		orderRepo: repo,
	}
}

func (o *orderUsecase) ListOrders(ctx context.Context, search *domain.SearchOrderList) ([]*domain.Order, int, error) {
	//TODO implement me
	panic("implement me")
}

func (o *orderUsecase) GetOrder(ctx context.Context, id int64, fields []string) (*domain.Order, error) {
	return o.orderRepo.GetOrder(ctx, id, fields)
}

func (o *orderUsecase) CreateOrder(ctx context.Context, do *domain.Order) (*domain.Order, error) {
	do.Id = time.Now().Unix()
	do.OrderSn = fmt.Sprintf("D%d", do.Id)
	for _, barcode := range do.Barcodes {
		barcode.Id = time.Now().Unix()
		for _, item := range barcode.Items {
			item.Id = time.Now().Unix()
		}
	}
	if do.Ext != nil {
		do.Ext.Id = time.Now().Unix()
	}
	for _, original := range do.Originals {
		original.Id = time.Now().Unix()
	}
	for _, user := range do.Users {
		user.Id = time.Now().Unix()
	}
	return o.orderRepo.CreateOrder(ctx, do)
}

func (o *orderUsecase) UpdateOrder(ctx context.Context, do *domain.Order, mask []string) (*domain.Order, error) {
	//TODO implement me
	panic("implement me")
}

func (o *orderUsecase) DeleteOrder(ctx context.Context, id int64) error {
	//TODO implement me
	panic("implement me")
}
